<template>
  <div class="base-layout">
    <DiyBoxLayout>
      <template v-slot:table>
        <DiyTable
          ref="table"
          :tableName="tableName"
          :tableHead="tableHead"
          :options="options"
          :tableData="tableData"
          @handleUpdate="loadData"
          ><template v-slot:buttons>
            <el-button type="text" @click="back">返回</el-button>
            <el-button type="text" @click="handleDeletes">删除</el-button>
            <el-button
              type="text"
              @click="$router.push({ name: 'departPerson-import' })"
              >部门批量导入</el-button
            >
            <el-button type="text" @click="handleAdd">新增</el-button>
          </template>
          <template v-slot:operate="scope">
            <TheTableButton
              :index="scope.index"
              :row="scope.row"
              @handleEdit="handleEdit"
              @handleDelete="handleDelete"
            ></TheTableButton>
          </template>
          <template v-slot:icon="scope">
            <i :class="scope.row.source"></i>
          </template>
        </DiyTable>
      </template>
    </DiyBoxLayout>

    <template v-for="item in dialogShow">
      <TheEditDialog
        v-if="item.key === 'edit' && item.value"
        :key="item.key"
        :dialogShow="item"
        @handleUpdate="loadData"
        @closeDialog="handleCloseDialog"
      ></TheEditDialog>
      <TheAddDialog
        v-if="item.key === 'add' && item.value"
        :key="item.key"
        :dialogShow="item"
        @handleUpdate="loadData"
        @closeDialog="handleCloseDialog"
      ></TheAddDialog>
    </template>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  getDepartmentList,
  deleteDepartment,
  getDepartmentTree,
  searchDeptTree,
} from "@api/department";
import DiyTable from "@components/DiyTable/index";
import TheEditDialog from "./TheEditDialog";
import TheAddDialog from "./TheAddDialog";
import DiyBoxLayout from "@layout/DiyBoxLayout/index";
import TheTableButton from "./TheTableButton";
export default {
  data() {
    let _this = this;
    return {
      dialogShow: [
        {
          key: "edit",
          value: false,
        },
        {
          key: "add",
          value: false,
        },
      ],
      tableName: "部门列表",
      tableData: [],
      tableHead: [
        {
          prop: "agencyName",
          label: "部门名称",
          width: "300",
        },
        {
          prop: "agencyCode",
          label: "部门编码",
          width: "200",
        },
        {
          prop: "updateTime",
          label: "更新时间",
          minWidth: "180",
        },
        {
          type: "slot",
          label: "操作",
          fixed: "right",
          align: "center",
          width: "80",
          name: "operate",
        },
      ],
      options: {
        checkBox: true,
        order: true,
        defaultSort: { prop: "createTime", order: "descending" },
        treeProps: {
          children: "children",
          hasChildren: "below",
        },
        noPage: true,
      },
      filterText: "",
    };
  },
  components: {
    DiyTable,
    TheEditDialog,
    TheAddDialog,
    DiyBoxLayout,
    TheTableButton,
  },
  created() {
    this.$nextTick(() => {
      this.loadData();
    });
  },
  mounted() {},
  computed: {
    ...mapGetters("tenant", ["curTenantCode", "curTenant"]),
    getFilterText() {
      return this.$store.state.searchContent;
    },
  },
  watch: {
    getFilterText(val) {
      this.filterText = val;
      if(val == ''){
        this.loadSearch(val, false);
      }else{
        this.loadSearch(val, true);
      }
      
    },
  },
  methods: {
    loadSearch(val, bool) {
      let params = {
        name: val,
      };
      searchDeptTree(params).then((res) => {
        this.tableData = res.data.data;
        this.handleExpand(this.tableData, bool);
      });
    },
    handleExpand(list, bool) {
      list.forEach((item) => {
        this.$refs.table.$refs.diyTable.toggleRowExpansion(item, bool);
        if(item.children.length !== 0){
          this.handleExpand(item.children)
        }
      });
    },
    back() {
      this.$router.go(-1);
    },
    loadData() {
      this.$refs.table.loading = true;
      const { currentPage, pageSize } = this.$refs.table.page;
      console.log("currentPage", currentPage);
      let params = {
        tenantCode: this.curTenantCode,
      };
      getDepartmentTree(params).then((res) => {
        let resList = res.data.data[0].children;
        this.tableData = resList;
        this.$refs.table.loading = false;
        let tableDataList = this.$refs.table;
        if (
          tableDataList.tableData.length == 0 &&
          tableDataList.page.currentPage >= 2
        ) {
          this.$refs.table.page.currentPage -= 1;
          this.loadData();
        }
      });
    },
    // loadData() {
    //   this.$refs.table.loading = true;
    //   const { currentPage, pageSize } = this.$refs.table.page;
    //   console.log("currentPage", currentPage);
    //   let params = {
    //     current: currentPage,
    //     size: pageSize,
    //     tenantCode: this.curTenantCode,
    //     fuzzyVal: this.filterText,
    //   };
    //   getDepartmentList(params).then((res) => {
    //     const { records, total } = res.data.data;
    //     this.tableData = records;
    //     this.$refs.table.page.total = total;
    //     this.$refs.table.loading = false;
    //     let tableData = this.$refs.table;
    //     if (
    //       tableData.tableData.length == 0 &&
    //       tableData.page.currentPage >= 2
    //     ) {
    //       this.$refs.table.page.currentPage -= 1;
    //       this.loadData();
    //     }
    //   });
    // },
    handleAdd() {
      this.dialogShow.map((el) => {
        if (el.key === "add") {
          el.value = true;
        }
      });
    },

    handleDeletes() {
      let ids = [];
      this.$refs.table.multipleSelection.forEach((element) => {
        ids.push(element.id);
      });
      if (ids.length === 0) {
        return this.$message.warning("请至少选择一个部门");
      } else {
        this.$confirm("此操作将永久删除该部门, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            let params = {
              ids: ids.join(","),
            };
            deleteDepartment(params).then((res) => {
              this.loadData();
              this.$message.success("删除部门成功");
            });
          })
          .catch(() => {
            return;
          });
      }
    },

    handleEdit(index, row) {
      this.dialogShow.map((el) => {
        if (el.key === "edit") {
          el.value = true;
          el.sendData = row;
        }
      });
    },

    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该部门, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = {
            ids: row.id,
          };
          deleteDepartment(params).then((res) => {
            this.loadData();
            this.$message.success("删除部门成功");
          });
        })
        .catch(() => {
          return;
        });
    },
  },
};
</script>

<style lang="scss" scoped></style>
